package com.mentalhealthplatform.global.rbac.dto.normal;


import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.mentalhealthplatform.global.rbac.model.Role;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.Set;

@Setter
@Getter
public class RoleDTO {
    @ExcelProperty("角色ID")
    private Long id;
    @ExcelProperty("角色标识符")
    private String identifier;
    @ExcelProperty("角色显示名称")
    private String displayName;
    @ExcelProperty("显示顺序")
    private int orderNum;
    @ExcelIgnore
    private LocalDateTime createdTime;
    @ExcelProperty("状态")
    private boolean status;
    @ExcelIgnore
    private Set<PermissionDTO> permissions;

    public RoleDTO() {
    }

    // 使用 Role 实体构造 RoleDTO
    public RoleDTO(Role role) {
        this.id = role.getId();
        this.identifier = role.getIdentifier();
        this.displayName = role.getDisplayName();
        this.orderNum = role.getOrderNum();
        this.createdTime = role.getCreatedTime();
        this.status = role.isEnabled();
    }

    // 通过所有字段的构造函数
    public RoleDTO(Long id, String identifier, String displayName, int orderNum, LocalDateTime createdTime, boolean status) {
        this.id = id;
        this.identifier = identifier;
        this.displayName = displayName;
        this.orderNum = orderNum;
        this.createdTime = createdTime;
        this.status = status;
    }

    // 静态方法将 Role 实体转换为 RoleDTO
    public static RoleDTO fromRole(Role role) {
        return new RoleDTO(
                role.getId(),
                role.getIdentifier(),
                role.getDisplayName(),
                role.getOrderNum(),
                role.getCreatedTime(),
                role.isEnabled()
        );
    }

    public static RoleDTO convertToDTO(Role role) {
        return new RoleDTO(
                role.getId(),
                role.getIdentifier(),
                role.getDisplayName(),
                role.getOrderNum(),
                role.getCreatedTime(),
                role.isStatus()
        );
    }

    // 新增：将 RoleDTO 转换为 Role 实体
    public Role convertToEntity() {
        Role role = new Role();
        role.setId(this.id);  // 设置角色ID
        role.setIdentifier(this.identifier);  // 设置角色标识符
        role.setDisplayName(this.displayName);  // 设置角色显示名称
        role.setOrderNum(this.orderNum);  // 设置排序号
        role.setCreatedTime(this.createdTime);  // 设置创建时间
        role.setStatus(this.status);  // 设置角色状态

        return role;
    }
}
